function login() {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    if (!username || !password) {
        errorMessage.textContent = '用户名和密码不能为空';
        return;
    }

    // 创建表单数据对象
    const formData = new FormData();
    formData.append('username', username);
    formData.append('password', password);

    // 使用fetch提交表单
    fetch('/login', { 
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        
        if (data.success) {
            alert('登录成功');
            // 登录成功后的操作
            window.location.href = '/user'; 
        } else {
            alert('登录失败：' + data.message);
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('登录失败');
    });
}
document.addEventListener('DOMContentLoaded', function() {
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');

    // 从 URL 参数中获取用户名
    const urlParams = new URLSearchParams(window.location.search);
    const username = urlParams.get('username');

    if (username) {
        usernameInput.value = username;
        passwordInput.focus();
    }
});